package com.insuremonitor.web.controller.system;

import com.insuremonitor.common.annotation.Log;
import com.insuremonitor.common.core.controller.BaseController;
import com.insuremonitor.common.core.domain.AjaxResult;
import com.insuremonitor.common.core.domain.entity.MonitorUser;
import com.insuremonitor.common.core.domain.model.LoginUser;
import com.insuremonitor.common.core.domain.model.UpdatePwdBody;
import com.insuremonitor.common.enums.BusinessType;
import com.insuremonitor.common.utils.SecurityUtils;
import com.insuremonitor.framework.web.service.TokenService;
import com.insuremonitor.system.service.IMonitorUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 个人信息 业务处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController
{
    @Autowired
    private IMonitorUserService monitorUserService;

    @Autowired
    private TokenService tokenService;

    @GetMapping
    public AjaxResult profile()
    {
        LoginUser loginUser = getLoginUser();
        MonitorUser user = loginUser.getMonitorUser();
        AjaxResult ajax = AjaxResult.success(user);
       // ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
       // ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
        return ajax;
    }



    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateProfile(@RequestBody MonitorUser user)
    {
        LoginUser loginUser = getLoginUser();
        MonitorUser currentUser = loginUser.getMonitorUser();
        currentUser.setNickName(user.getNickName());

        if (monitorUserService.updateUserProfile(currentUser) > 0)
        {
            // 更新缓存用户信息
            tokenService.setLoginUser(loginUser);
            return success();
        }
        return error("修改个人信息异常，请联系管理员");
    }


    /**
     * 修改用户手机号
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePhone")
    public AjaxResult updatePhone(@RequestBody MonitorUser user)
    {
        LoginUser loginUser = getLoginUser();
        MonitorUser currentUser = loginUser.getMonitorUser();
        currentUser.setNickName(user.getNickName());

        if (monitorUserService.updateUserProfile(currentUser) > 0)
        {
            // 更新缓存用户信息
            tokenService.setLoginUser(loginUser);
            return success();
        }
        return error("修改个人信息异常，请联系管理员");
    }


    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword)
    {
        LoginUser loginUser = getLoginUser();
        String userName = loginUser.getUsername();
        String password = loginUser.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password))
        {
            return error("修改密码失败，旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newPassword, password))
        {
            return error("新密码不能与旧密码相同");
        }
        if (monitorUserService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword)) > 0)
        {
            // 更新缓存用户密码
            loginUser.getMonitorUser().setPassword(SecurityUtils.encryptPassword(newPassword));
            tokenService.setLoginUser(loginUser);
            return success();
        }
        return error("修改密码异常，请联系管理员");
    }





}
